<?php
    session_start();
    require_once '../includes/runtime/lib/Propel.php';
    Propel::init("../includes/build/conf/easylib-conf.php");
    set_include_path("../includes/build/classes" . PATH_SEPARATOR . get_include_path());
    if(isset($_SESSION["admin_login"])&&isset($_SESSION["admin_key"])){
        $query = TabAdminQuery::create()->filterByLogin($_SESSION["admin_login"])->findOne();
        if($query!=null){
            if(md5($query->getPass())==$_SESSION["admin_key"]){
                switch ($_GET["action"]){
                    case "liste":
                        $admins = TabAdminQuery::create()->find();
                        $results = array();
                        foreach ($admins as $admin){
                            $admin instanceof TabAdmin;
                            $id = $admin->getIdAdmin();
                            $droit_admin = $admin->getTabDroit();
                            $droit = $droit_admin->getNom();
                            $nom = $admin->getNom();
                            $prenom = $admin->getPrenom();
                            $email = $admin->getEmail();
                            $login = $admin->getLogin();
                            $results[] = array(
                                "id" => $id,
                                "nom" => $nom,
                                "prenom" => $prenom,
                                "email" => $email,
                                "login" => $login,
                                "droit" => $droit,
                                "id_droit" => $admin->getFkDroit()
                            );
                        }
                        echo json_encode($results);
                        break;
                    case "supprimer":
                        $admin = TabAdminQuery::create()->filterByIdAdmin($_GET["id"])->findOne();
                        $admin->delete();
                        break;
                    case "liste_droit":
                        $droits = TabDroitQuery::create()->find();
                        echo $droits->toJSON();
                        break;
                    case "ajouter":
                        $query = TabAdminQuery::create()->filterByLogin($_GET["login"])->findOne();
                        if($query!=null)
                            break;
                        $query = TabAdminQuery::create()->filterByEmail($_GET["email"])->findOne();
                        if($query!=null)
                            break;
                        $nom = $_GET["nom"];
                        $prenom = $_GET["prenom"];
                        $email = $_GET["email"];
                        $login = $_GET["login"];
                        $id_droit = $_GET["droit"];
                        $admin = new TabAdmin();
                        $admin->setNom($nom);
                        $admin->setPrenom($prenom);
                        $admin->setEmail($email);
                        $admin->setLogin($login);
                        $admin->setFkDroit($id_droit);
                        $admin->setPass(random(8));
                        $admin->save();
                        require_once '../includes/mail.class.php';
                        $mail = new mail(null,null,null);
                        $mail->setDestinataires($admin->getEmail());
                        $mail->setObjet("EasyLib - Rappel des identifiants Admin");
                        $mail->setContenu("<h1>EasyLib - Rappel des identifiants Admin</h1>
                            Login : <b>".$admin->getLogin()."</b><br />
                            Mot de passe : <b>".$admin->getPass()."</b>
                            ");
                        $mail->send();
                        break;
                    case "modifier":
                        $admin = TabAdminQuery::create()->filterByIdAdmin($_GET["id_admin"])->findOne();
                        if($admin==null)
                            break;
                        $nom = $_GET["nom"];
                        $prenom = $_GET["prenom"];
                        $email = $_GET["email"];
                        $login = $_GET["login"];
                        $id_droit = $_GET["droit"];
                        $admin->setNom($nom);
                        $admin->setPrenom($prenom);
                        $admin->setEmail($email);
                        $admin->setLogin($login);
                        $admin->setFkDroit($id_droit);
                        $admin->save();
                        break;
                }
            }
        }
    }
    
    function random($car) {
        $string = "";
        $chaine = "abcdefghijklmnpqrstuvwxy";
        srand((double)microtime()*1000000);
        for($i=0; $i<$car; $i++) {
            $string .= $chaine[rand()%strlen($chaine)];
        }
        return $string;
    }
?>
